"""
Script manager module for HeyGen video generator.
Handles script content for avatar videos.
"""

import sys
import os

# Add the project root directory to the Python path
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

from playwright.async_api import Page

# Use absolute import for avatar_data
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
import avatar_data


class ScriptManager:
    """
    Manages script content for HeyGen avatar videos.
    """
    
    async def add_script(self, page: Page, avatar_name: str) -> bool:
        """
        Add script content to the video.
        
        Args:
            page (Page): Playwright page instance
            avatar_name (str): Name of the avatar
            
        Returns:
            bool: True if script added successfully, False otherwise
        """
        try:
            # Wait for page to update
            await page.wait_for_timeout(3000)
            
            # Add script content (randomly selected based on avatar)
            script_text = avatar_data.get_random_script_for_avatar(avatar_name)
            print(f"Using script: {script_text}")

            await page.fill("#create-video-modal-script", script_text)
            print("Filled script text")
            return True
        except Exception as e:
            print(f"Failed to add script: {e}")
            return False